# -*- coding: utf-8 -*-
from odoo import models, api, fields, tools
from odoo.exceptions import UserError
import re


class pro_plan_task(models.Model):

    _name = "proman.pro_plan_task"
    _description = "建设内容"
    _table = "proman_pro_plan_task"
    _order = "sequence"

    sequence = fields.Integer(string="排序",translate = 0)
    name = fields.Char(string="计划名称",help = '请输入计划名称',translate = 0)
    main_id = fields.Many2one(string="主单据号",comodel_name = 'proman.pro_plan',ondelete = 'cascade',translate = 0)
    company_id = fields.Many2one(string="所属公司",comodel_name = 'res.company',related = 'main_id.company_id',default = lambda self:self.env.company,required = 1,store = 1,translate = 0)
    line_project_category_id = fields.Many2one(string="项目类型",comodel_name = 'proman.project_category',related = 'main_id.project_category_id',store = 1,translate = 0)
    line_project_stage_id = fields.Many2one(string="项目阶段",comodel_name = 'proman.stage_category',related = 'main_id.project_stage_id',store = 1,translate = 0)
    line_project_id = fields.Many2one(string="项目名称",comodel_name = 'proman.project_base',related = 'main_id.project_id',store = 1,translate = 0)
    line_year = fields.Date(string="年度",related = 'main_id.year',store = 1,translate = 0)
    line_partner_id = fields.Many2one(string="相关单位",comodel_name = 'res.partner',related = 'main_id.partner_id',domain = [('is_company','=',True)],store = 1,translate = 0)
    line_gogal = fields.Text(string="年度目标",related = 'main_id.gogal',store = 1,translate = 0)
    line_manage_department_id = fields.Many2one(string="管理机构",comodel_name = 'hr.department',related = 'main_id.manage_department_id',store = 1,translate = 0)
    indicator_category_id = fields.Many2one(string="指标分类",comodel_name = 'proman.task_category',related = 'indicator_id.indicator_category_id',readonly = 1,store = 1,translate = 0)
    indicator_id = fields.Many2one(string="任务内容",comodel_name = 'proman.task_indicator',translate = 0)
    uom_id = fields.Many2one(string="计量单位",comodel_name = 'uom.uom',related = 'indicator_id.uom_id',store = 1,translate = 0)
    quantity = fields.Float(string="数量",help = '请输入数量',translate = 0)
    currency_id = fields.Many2one(string="货币",comodel_name = 'res.currency',default = lambda self:self.env['res.currency'].search([('name','=','CNY')]),translate = 0)
    price = fields.Monetary(string="单价",help = '请输入单价',translate = 0)
    amount = fields.Monetary(string="金额",help = '请输入金额',compute = '_compute_amount',translate = 0)
    payment_category_id = fields.Many2one(string="支付类别",comodel_name = 'proman.payment_category',translate = 0)
    payment_rate = fields.Float(string="支付比例",translate = 0)
    depreciation_category_id = fields.Many2one(string="折旧类别",comodel_name = 'proman.depreciation_category',translate = 0)
    depreciation_rate = fields.Float(string="折旧率",translate = 0)
    memo = fields.Text(string="备注说明",translate = 0)

    @api.depends("quantity", "price")
    def _compute_amount(self):
        for record in self:
            record.amount = record.quantity * record.price

